package cn.wen.eeayjoke.net.observer;

import android.app.Activity;
import android.content.DialogInterface;

import cn.wen.eeayjoke.dialog.LoadingDialog;
import io.reactivex.disposables.Disposable;


/**
 * 弹出Dialog的网络封装。
 */

public abstract class DialogObserver<T> extends QuickObserver<T> {
    private LoadingDialog mLoadingDialog;
    private String text;

    public DialogObserver(Activity activity, String text) {
        super(activity);
        this.text = text;
        mLoadingDialog = new LoadingDialog(activity);
        mLoadingDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                // 取消请求
                cancel();
            }
        });
    }

    public DialogObserver(Activity activity) {
        this(activity, "");
    }


    @Override
    public void onSubscribe(Disposable d) {
        super.onSubscribe(d);
        mLoadingDialog.show(text);
    }

    @Override
    public void onComplete() {
        dismissDialog();
    }

    @Override
    public void onError(Throwable e) {
        super.onError(e);
        dismissDialog();
    }

    private void dismissDialog() {
        if (mLoadingDialog != null) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }
}
